package top.raoqq.sky.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import top.raoqq.sky.entity.sql.User;
import top.raoqq.sky.enums.SkyResponseEnum;
import top.raoqq.sky.exception.BizException;
import top.raoqq.sky.service.UserService;
import top.raoqq.sky.vo.Result;
import top.raoqq.sky.vo.UserQueryVO;
import javax.validation.Valid;
import java.time.LocalDate;
import java.util.Objects;


/**
 * @author 饶奇奇
 * @date 2023/8/11
 */
@RestController
@Api(tags = "用户管理")
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserController {
    private final UserService userService;

    /**
     * 分页条件查询用户
     */
    private static final String LIST_USER_CONDITION_PAGE = "list/{pageNumber}/{pageSize}";

    /**
     * 保存或者修改用户
     */
    private static final String SAVE_OR_UPDATE_USER = "saveOrUpdate";

    /**
     * 根据主键删除用户
     */
    private static final String REMOVE_USER_BY_ID = "remove/{id}";

    /**
     * 切换用户的状态
     */
    private static final String SWITCH_USER_STATUS = "updateStatus/{userId}/{status}";

    @GetMapping(LIST_USER_CONDITION_PAGE)
    @ApiOperation("分页条件查询用户列表")
    public Result<Page<User>> listUserPage(@PathVariable  Long pageNumber,
                               @PathVariable Long pageSize,
                               @Valid UserQueryVO userQueryVo) {
        LocalDate startTime = userQueryVo.getStartTime();
        LocalDate endTime = userQueryVo.getEndTime();
        if (Objects.nonNull(startTime) && Objects.nonNull(endTime)) {
            // 开始时间大于结束时间
            if (startTime.compareTo(endTime) > 0) {
                throw new BizException(SkyResponseEnum.END_TIME_NOT_CAN_LT_START_TIME);
            }
        }
        Page<User> page = new Page<>(pageNumber, pageSize);
        userService.listUserPageCondition(page,
                userQueryVo.getUsername(),
                userQueryVo.getPhone(),
                userQueryVo.getStatus(),
                userQueryVo.getStartTime(),
                userQueryVo.getEndTime());

        return Result.success(page);
    }

    @PostMapping(SAVE_OR_UPDATE_USER)
    @ApiOperation("保存用户")
    public Result<Void> saveOrUpdateUser(@RequestBody @Valid User user) {
        userService.saveOrUpdateUser(user);
        return Result.success();
    }

    @DeleteMapping(REMOVE_USER_BY_ID)
    public Result<Void> removeUserById(@PathVariable Long id) {
        // 参数校验
        if (id <= 0) {
            throw new BizException(SkyResponseEnum.ARGUMENTS_ERROR);
        }
        userService.removeById(id);
        return Result.success();
    }

    @PutMapping(SWITCH_USER_STATUS)
    public Result<Void> switchUserStatus(@PathVariable Long userId, @PathVariable Byte status) {
        // 参数校验
        if (userId <= 0 || status > 1 || status < 0) {
            throw new BizException(SkyResponseEnum.ARGUMENTS_ERROR);
        }
        // 切换用户状态
        userService.switchUserStatus(userId, status);
        return Result.success();
    }

}
